package com.hytc.dao.impl;


import com.hytc.dao.UserDao;
import com.hytc.entity.Cart;
import com.hytc.entity.Product;
import com.hytc.entity.User;

import java.util.List;

public class UserDaoImpl extends BaseDao implements UserDao {


    public User queryByUsername(String username) {
        String sql = "select * from user where username = ?";
        return queryForOne(User.class, sql, username);
    }

    @Override
    public User queryByUsernameAndPassword(String username, String password) {
        String sql = "select * from user where username = ? and password = ?";
        return queryForOne(User.class, sql, username, password);
    }

    @Override
    public int saveUser(User user) {
        String sql = "insert into user values(?,?,?,?,?,?,?,?,?)";
        return update(sql,
                user.getId(),
                user.getUsername(),
                user.getPassword(),
                user.getEmail(),
                user.getGender(),
                user.getHobby(),
                user.getCreateTime(),
                user.getUpdateTime(),
                user.getState()
        );
    }

    @Override
    public List<Product> queryBycategory_id() {
        String sql = "select * from commodity_classify";
        return queryForList(Product.class, sql);
    }

    @Override
    public List<User> find() {
        String sql = "select * from user";
        return queryForList(User.class, sql);
    }

    @Override
    public List<Cart> cart(Long id) {
        String sql ="select * from buy where user_id=?";
        return queryForList(Cart.class,sql,id);
    }



    @Override
    public boolean add(int id, Long user_id) {
        String sql = "select *from buy where commodity_id=? and user_id=?";
        if (queryForOne(Cart.class, sql, id, user_id) != null){
            return false;
        }else return true;
    }


    @Override
    public boolean delete(int id, int user_id) {
        String sql = "delete  from buy where commodity_id=? and user_id=?";
        deletecart(sql,id,user_id);
        return true;
    }

    @Override
    public void addcart(Product product, String mount, Long id) {
        String sql = "insert into buy value(?,?,?,?,?,?,?,?)";
        update(sql,null,product.getCommodity_name(),product.getPrice(),product.getPrice()*product.getDiscount(),product.getDiscount(),mount,id,product.getId());

    }


}
